package cn.migu.music.hangupsms.service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.migu.music.common.redis.IRedisClient;
import cn.migu.music.hangupsms.dbcache.BlackUserCacheManager;
import cn.migu.music.hangupsms.dbcache.SentSmsCacheManager;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
public class SendRecordCacheService {
    @Autowired
    private SentSmsCacheManager            sentSmsCacheManager;
    @Autowired
    private BlackUserCacheManager          blackUserCacheManager;
    @Autowired
    private IRedisClient                   redisClient;
    private static final String            SECOND_FORMAT = "yyyyMMddHHmmss";
    private static final DateTimeFormatter DFT           = DateTimeFormatter.ofPattern(SECOND_FORMAT);

    /**
     * 操作短信缓存
     * 
     * @param op
     *            操作类型
     * @return
     */
    @GetMapping({ "/hangupsms/sms-record-cache/v1.0" })
    public Map<String, Object> getSmsSentStatic(@RequestParam("op") String op) {
        Map<String, Object> ret = new HashMap<>();
        if (StringUtils.equals("static", op)) {
            ret.putAll(sentSmsCacheManager.getStatic());
            return ret;
        }
        if (StringUtils.equals("reset", op)) {
            sentSmsCacheManager.resetStatic();
        }
        if (StringUtils.startsWith(op, "p-")) {
            Set<String> s = sentSmsCacheManager.getRedisValue(op.substring(2));
            ret.put("redisValue", s);
            return ret;
        }
        return ret;
    }

    /**
     * 操作短信缓存load-data
     * 
     * @param op
     *            操作类型
     * @return
     */
    @GetMapping({ "/hangupsms/sms-record-cache/load-data/v1.0" })
    public String loadData(@RequestParam("op") String op) {
        if (StringUtils.equals("run", op)) {
            sentSmsCacheManager.loadDbData();
        } else if (StringUtils.equals("stop", op)) {
            sentSmsCacheManager.interruptLoadData();
        } else if (StringUtils.equals("getLock", op)) {
            String flag = sentSmsCacheManager.getLock();
            return "lock_flag:" + (flag == null ? "nil" : flag);
        } else if (StringUtils.equals("delLock", op)) {
            sentSmsCacheManager.deleteLock();
        } else {
            return "done";
        }
        return "done";
    }

    /**
     * 未删除上月过期数据
     * 
     * @param phone
     *            号码
     * @param now
     *            时间
     * @return
     */
    @GetMapping({ "/hangupsms/sms-record-cache/expire/v1.0" })
    public String keyExpire(@RequestParam("phone") String phone, @RequestParam("now") String now) {
        LocalDateTime dateTime = LocalDateTime.parse(now, DFT);
        sentSmsCacheManager.setExpire("hangup_sms_record_set_" + phone, dateTime);
        return "done";
    }

    /**
     * 暂停任务
     * 
     * @return
     */
    @GetMapping({ "/hangupsms/blackuser/task/stop/v1.0" })
    public String stopBlackUserTask() {
        blackUserCacheManager.stopLoading();
        return "done";
    }
}
